﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Negocio;
using System.Windows.Controls.Primitives;
using Microsoft.Win32;
using System.Windows.Media.Imaging;
namespace Presentacion
{
    public partial class WPlayer
    {
        private Club club;
        public static WPlayer instance;
        private static bool openOnce;
        private bool insertPlayer;
        private Grid principalGrid;
        private string photo;

        private WPlayer(Grid onePrincipalGrid)
        {
            this.InitializeComponent();
            listViewPlayers.AddHandler(Thumb.DragDeltaEvent,
   new DragDeltaEventHandler(listViewPlayers_DragDelta),
   true);
            principalGrid = onePrincipalGrid;
            club = Club.getInstance();
            this.Loaded += new RoutedEventHandler(OnLoad);
            openOnce = false;            
            insertPlayer = true;
            cbxPlayerFilter.SelectedItem = cbiAll;
            photo = "";
        }

        public static WPlayer getInstance(Grid onePrincipalGrid)
        {
            if (instance == null)
            {
                instance = new WPlayer(onePrincipalGrid);
            }
            return instance;
        }

        void OnLoad(object sender, RoutedEventArgs e)
        {
            listViewPlayers.ItemsSource = club.Players;
            openOnce = true;
            //this.listViewPlayers.SelectedIndex = -1;
            cleanAllTextbox();
            this.txbCI.Focus();
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            if (insertPlayer)
            {
                Player player = club.newPlayer(txbName.Text, txbLastName.Text, txbLastName2.Text, txbCI.Text, dpBirthDate.SelectedDate.Value, dpEnterDate.SelectedDate.Value, txbWeight.Text, txbClub.Text, txbHeight.Text, chkbEnabled.IsEnabled, cbxPosition.Text, cbxLeg.Text, photo);
                if (player != null)
                {
                    if (!club.addPlayer(player) == true)
                    {
                        MessageBox.Show("El Jugador ya fue ingresado al sistema", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        cleanAllTextbox();
                    }
                }
                else
                {
                    MessageBox.Show("Datos incorrectos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                txbCI.Focus();
            }
            else 
            {
                Player player = (Player)listViewPlayers.SelectedItem;
                if (player != null)
                {
                    player = club.updatePlayer(txbName.Text, txbLastName.Text, txbLastName2.Text, txbCI.Text, dpBirthDate.SelectedDate.Value, dpEnterDate.SelectedDate.Value, txbWeight.Text, txbClub.Text, txbHeight.Text, chkbEnabled.IsChecked.Value, cbxPosition.Text, cbxLeg.Text,photo,player);

                    if (player != null)
                    {
                        club.Players[listViewPlayers.SelectedIndex] = player;
                        cleanAllTextbox();                        
                        listViewPlayers.ItemsSource = null;
                        changeSources();
                    }
                    else
                    {
                        MessageBox.Show("Datos incorrectos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        txbCI.Focus();
                    }
                }
            }
            
        }

        private void cleanAllTextbox()
        {
            txbName.Text = "";
            txbLastName.Text = "";
            txbLastName2.Text = "";
            txbCI.Text = "";
            txbWeight.Text = "0";
            txbClub.Text = "";
            txbHeight.Text = "0";
            chkbEnabled.IsChecked = true;
            dpBirthDate.SelectedDate = DateTime.Today;
            dpEnterDate.SelectedDate = DateTime.Today;
            cbxLeg.Text = "";
            cbxPosition.Text = "";
            chkbEnabled.IsChecked = true;
            this.listViewPlayers.SelectedIndex = -1;            
            insertPlayer = true;
            photo = "";
            imgPlayer.Source = new BitmapImage(new Uri("C:\\Users\\Pablo\\Desktop\\Codigo29-10\\Presentacion\\Presentacion\\Foto en Blanco.png"));
            
        }
                
        private void listViewPlayers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (openOnce)
            {
                int pos = listViewPlayers.SelectedIndex;
                if (this.listViewPlayers.SelectedIndex != -1)
                {
                    if (cbxPlayerFilter.SelectedItem.Equals(cbiEnabled))
                    {   
                        txbName.Text = club.loadEnabledPlayers()[pos].Name;
                        txbLastName.Text = club.loadEnabledPlayers()[pos].FirstLastname;
                        txbLastName2.Text = club.loadEnabledPlayers()[pos].SecondLastname;
                        txbCI.Text = club.loadEnabledPlayers()[pos].IdentityDocument.ToString();
                        txbWeight.Text = club.loadEnabledPlayers()[pos].Weight.ToString();
                        txbClub.Text = club.loadEnabledPlayers()[pos].Club.ToString();
                        txbHeight.Text = club.loadEnabledPlayers()[pos].Height.ToString();
                        chkbEnabled.IsChecked = club.loadEnabledPlayers()[pos].Enabled;
                        dpBirthDate.SelectedDate = club.loadEnabledPlayers()[pos].BirthDate;
                        dpEnterDate.SelectedDate = club.loadEnabledPlayers()[pos].AdmissionDate;
                        cbxPosition.Text = club.loadEnabledPlayers()[pos].Position;
                        cbxLeg.Text = club.loadEnabledPlayers()[pos].Leg;
                        try
                        {
                            imgPlayer.Source = new BitmapImage(new Uri(club.loadEnabledPlayers()[pos].Photo));
                        }
                        catch (Exception)
                        {
                            imgPlayer.Source = new BitmapImage(new Uri("C:\\Users\\Pablo\\Desktop\\Codigo29-10\\Presentacion\\Presentacion\\Foto en Blanco.png"));
            
                        }                        
                    }
                    if (cbxPlayerFilter.SelectedItem.Equals(cbiAll))
                    {
                        txbName.Text = club.Players[pos].Name;
                        txbLastName.Text = club.Players[pos].FirstLastname;
                        txbLastName2.Text = club.Players[pos].SecondLastname;
                        txbCI.Text = club.Players[pos].IdentityDocument.ToString();
                        txbWeight.Text = club.Players[pos].Weight.ToString();
                        txbClub.Text = club.Players[pos].Club.ToString();
                        txbHeight.Text = club.Players[pos].Height.ToString();
                        chkbEnabled.IsChecked = club.Players[pos].Enabled;
                        dpBirthDate.SelectedDate = club.Players[pos].BirthDate;
                        dpEnterDate.SelectedDate = club.Players[pos].AdmissionDate;
                        cbxPosition.Text = club.Players[pos].Position;
                        cbxLeg.Text = club.Players[pos].Leg;
                        try
                        {
                            imgPlayer.Source = new BitmapImage(new Uri(club.loadEnabledPlayers()[pos].Photo));
                        }
                        catch (Exception)
                        {
                            imgPlayer.Source = new BitmapImage(new Uri("C:\\Users\\Pablo\\Desktop\\Codigo29-10\\Presentacion\\Presentacion\\Foto en Blanco.png"));
            
                        }  
                    }
                    if (cbxPlayerFilter.SelectedItem.Equals(cbiDisabled))
                    {
                        txbName.Text = club.loadDisabledPlayers()[pos].Name;
                        txbLastName.Text = club.loadDisabledPlayers()[pos].FirstLastname;
                        txbLastName2.Text = club.loadDisabledPlayers()[pos].SecondLastname;
                        txbCI.Text = club.loadDisabledPlayers()[pos].IdentityDocument.ToString();
                        txbWeight.Text = club.loadDisabledPlayers()[pos].Weight.ToString();
                        txbClub.Text = club.loadDisabledPlayers()[pos].Club.ToString();
                        txbHeight.Text = club.loadDisabledPlayers()[pos].Height.ToString();
                        chkbEnabled.IsChecked = club.loadDisabledPlayers()[pos].Enabled;
                        dpBirthDate.SelectedDate = club.loadDisabledPlayers()[pos].BirthDate;
                        dpEnterDate.SelectedDate = club.loadDisabledPlayers()[pos].AdmissionDate;
                        cbxPosition.Text = club.loadDisabledPlayers()[pos].Position;
                        cbxLeg.Text = club.loadDisabledPlayers()[pos].Leg;
                        try
                        {
                            imgPlayer.Source = new BitmapImage(new Uri(club.loadEnabledPlayers()[pos].Photo));
                        }
                        catch (Exception)
                        {
                            imgPlayer.Source = new BitmapImage(new Uri("C:\\Users\\Pablo\\Desktop\\Codigo29-10\\Presentacion\\Presentacion\\Foto en Blanco.png"));
            
                        }  
                    }
                    insertPlayer = false;
                }
            }
        }

        private void dpBirthDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {                
                if (dpBirthDate.SelectedDate.Value > DateTime.Today)
                {
                    MessageBox.Show("La fecha de nacimiento no puede ser mayor a la actual", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    dpBirthDate.SelectedDate = DateTime.Today;
                }
                dpBirthDate.Focus();
            }
            catch (Exception)
            { }
        }

        private void dpEnterDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (dpEnterDate.SelectedDate.Value < dpBirthDate.SelectedDate.Value)
                {
                    MessageBox.Show("La fecha de ingreso no puede ser menor a la fecha de nacimiento", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    dpEnterDate.SelectedDate = DateTime.Today;
                }
                if (dpEnterDate.SelectedDate.Value > DateTime.Today)
                {
                    MessageBox.Show("La fecha de ingreso no puede ser mayor a la actual", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    dpEnterDate.SelectedDate = DateTime.Today;
                }
                dpEnterDate.Focus();
            }
            catch (Exception)
            { }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            cleanAllTextbox();
        }

        private void cbxPlayerFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            changeSources();
        }

        private void changeSources()
        {
            if (cbxPlayerFilter.SelectedItem.Equals(cbiEnabled))
            {
                listViewPlayers.ItemsSource = null;
                listViewPlayers.ItemsSource = club.loadEnabledPlayers();
            }
            if (cbxPlayerFilter.SelectedItem.Equals(cbiAll))
            {
                listViewPlayers.ItemsSource = null;
                listViewPlayers.ItemsSource = club.Players;
            }
            if (cbxPlayerFilter.SelectedItem.Equals(cbiDisabled))
            {
                listViewPlayers.ItemsSource = null;
                listViewPlayers.ItemsSource = club.loadDisabledPlayers();
            }
            cleanAllTextbox();
        }

        private void listViewPlayers_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Thumb senderAsThumb = e.OriginalSource as Thumb;
            GridViewColumnHeader header
              = senderAsThumb.TemplatedParent as GridViewColumnHeader;
            if (header != null)
            {
                if (header.Column.ActualWidth < 240)
                {
                    header.Column.Width = 213;
                }
                if (header.Column.ActualWidth > 240)
                {
                    header.Column.Width = 213;
                }
            }
        }

        private void btnClinicalHistory_Click(object sender, RoutedEventArgs e)
        {
            if (listViewPlayers.SelectedIndex != -1) 
            {
                ClinicalHistory clinicalHistory = club.getClinicalHistory((Player)listViewPlayers.SelectedItem);
                WClinicalHistory wClinicalHistory = new WClinicalHistory(clinicalHistory);
                principalGrid.Children.Clear();
                principalGrid.Children.Add(wClinicalHistory);
            
            }

        }

        private void imgPlayer_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                OpenFileDialog o = new OpenFileDialog();
                o.ShowDialog();
                photo = o.FileName.ToString();
                imgPlayer.Source = new BitmapImage(new Uri(photo));
            }
            catch (Exception) 
            {
                imgPlayer.Source = new BitmapImage(new Uri("C:\\Users\\Pablo\\Desktop\\Codigo29-10\\Presentacion\\Presentacion\\Foto en Blanco.png"));
            
            }
        }

        
    }


}

